#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
//#include <malloc.h>
//#include <unistd.h>


#include "common.h"
/*****************************************************************************
* 文件操作相关
*****************************************************************************/

int com_get_file_size(char *filename)
{
    if((NULL == filename)||(0 == strlen(filename)))
    {
        WD_ERR("the param is error!\n");
        return 0;
    }
    
    struct stat statbuf;
    memset((uint8 *)&statbuf, 0x00, sizeof(struct stat));
    stat(filename, &statbuf);
    int size = statbuf.st_size;

    return size;
}


int com_check_file_exit(char *file_path, int size_min, int size_max)
{
    int file_size = 0;

    if((NULL == file_path) || (0 >= strlen(file_path)))
    {
        return -1;
    }

    file_size = com_get_file_size(file_path);
    if((size_min > file_size) || (size_max < file_size))//此处最大文件大小待确认
    {
        WD_DBG("%s file_size = %d, error!\n", file_path, file_size);
        return -1;//文件不存在或文件错误
    }

    return 0;//文件存在
}



/*****************************************************************************
* Name:    com_InvertUint8
* Note:     把字节颠倒过来，如0x12变成0x48 0x12: 0001 0010 0x48: 0100 1000
*****************************************************************************/
void com_InvertUint8(unsigned char *dBuf,unsigned char *srcBuf)
{
    int i;
    unsigned char tmp[4]={0};
    for(i=0;i< 8;i++)
    {
        if(srcBuf[0]& (1 << i))
            tmp[0]|=1<<(7-i);
    }
    dBuf[0] = tmp[0];
}

/*****************************************************************************
* Name:    com_InvertUint16
* Note:     把双字节颠倒过来
*****************************************************************************/
void com_InvertUint16(unsigned short *dBuf,unsigned short *srcBuf)
{
    int i;
    unsigned short tmp[4]={0};
    for(i=0;i< 16;i++)
    {
        if(srcBuf[0]& (1 << i))
            tmp[0]|=1<<(15 - i);
    }
    dBuf[0] = tmp[0];
}

int com_compare_array(uint8 *src, uint8 *dst, int len)
{
    for(int i = 0; i < len; i++)
    {
        if(src[i] != dst[i])
        {
            return -1;
        }
    }
    return 0;
}

/****************************Info**********************************************
 * Name:    CRC-16/X25          x16+x12+x5+1
 * Width:   16
 * Poly:    0x1021
 * Init:    0xFFFF
 * Refin:   True
 * Refout:  True
 * Xorout:  0XFFFF
 * Note:
 *****************************************************************************/
#if 1
unsigned short com_CRC16_X25(char *data, unsigned int datalen)
{
    unsigned short wCRCin = 0xFFFF;
    unsigned short wCPoly = 0x1021;
    unsigned char wChar = 0;
    while (datalen--)
    {
        wChar = *(data++);
        com_InvertUint8(&wChar,&wChar);
        wCRCin ^= (wChar << 8);
        for(int i = 0;i < 8;i++)
        {
            if(wCRCin & 0x8000)
                wCRCin = (wCRCin << 1) ^ wCPoly;
            else
                wCRCin = wCRCin << 1;
        }
    }
    com_InvertUint16(&wCRCin,&wCRCin);
    return (wCRCin^0xFFFF);
}
#else
unsigned short com_CRC16_X25(char *data, unsigned int datalen)
{
    unsigned short wCRCin = 0xFFFF;
    unsigned short wCPoly = 0x1021;
    com_InvertUint16(&wCPoly,&wCPoly);
    while (datalen--)
    {
        wCRCin ^= *(data++);
        for(int i = 0;i < 8;i++)
        {
            if(wCRCin & 0x01)
                wCRCin = (wCRCin >> 1) ^ wCPoly;
            else
                wCRCin = wCRCin >> 1;
        }
    }
    return (wCRCin^0xFFFF);
}
#endif

/****************************Info**********************************************
* 异或校验
 *****************************************************************************/
uint8 com_Check_BCC(uint8 *data, int len)
{
    uint8 bcc = 0;

    if((NULL == data)||(0 == len))
    {
        WD_ERR("the param is error!!!\n");
        return 0xff;
    }

    for(int i = 0; i < len; i++)
    {
        bcc ^= data[i];
    }

    return bcc;
}



